const app = getApp();
const ajax = app.myRequest()
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'
Page({
    data: {
        order: [
            {id: 0,title: '全部',state: 'ALL'},
            {id: 1,title: '已超时',state: -1},
            {id: 2,title: '待付款',state: 0},
            {id: 3,title: '已付款',state: 1},
            {id: 4,title: '已使用',state: 2},
            {id: 5,title: '已到期',state: 3},
            {id: 6,title: '已退款',state: 4}
        ],
        value: 0,
        idx: 0
    },

    onLoad: function (options) {
        if (options.id) { this.setData({ idx: options.id }) }
        this.getOrder()
    },

    // 查看订单
    getOrder() {
        var index = this.data.idx
        var status = this.data.order[index].state
        var _this = this
        ajax.getRequest('/Order/index', { status }).then((res) => {
            _this.setData({
                allOrder: res.data.data.data,
                current_page: res.data.data.current_page
            })
        })
    },

    refund(e) {
        var index = e.currentTarget.dataset.value
        var OrderId = this.data.allOrder[index].id
        wx.showModal({
            title: '提示',
            content: '您确定要退款么？',
            success: function (res) {
                if (res.confirm == true) {
                    ajax.postRequest('/Order/refund', { id: OrderId }).then((res) => {
                        Dialog.alert({ message: res.data.msg })
                    })
                }
            }
        })
    },

    // 查看订单详情
    toOrderDetails(e) {
        var index = e.currentTarget.dataset.value
        var status = this.data.allOrder[index].status
        var id = this.data.allOrder[index].id
        wx.navigateTo({ url: '/pages/personal/orderDetails/index?status=' + status + '&id=' + id })
    },

    getNumber(e) {
        this.setData({ idx: e.target.dataset.idx })
        this.getOrder()
    },

    // 加载数据
    onReachBottom: function () {
        this.branch()
    },

    branch() {
        var _this = this
        var current_page = _this.data.current_page
        var index = this.data.idx
        var status = this.data.order[index].state

        ajax.getRequest('/Order/index', { page: current_page + 1, status }).then((res) => {
            var allOrder = _this.data.allOrder.concat(res.data.data.data)
            _this.setData({
                allOrder: allOrder,
                current_page: res.data.data.current_page,
                total: res.data.data.total
            })
        })
    },
})